package com.zhao.fishingguideserver.script;

import com.zhao.fishingguideserver.utils.FishImageBatchProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 鱼类图片批量上传脚本
 * 在应用启动时执行，用于批量上传鱼类图片到阿里云OSS
 */
@Component
@Slf4j
public class FishImageUploadScript implements CommandLineRunner {
    
    @Autowired
    private FishImageBatchProcessor batchProcessor;
    
    @Override
    public void run(String... args) throws Exception {
        // 检查是否需要执行图片上传
        if (args.length > 0 && "upload-fish-images".equals(args[0])) {
            log.info("开始执行鱼类图片批量上传...");
            uploadFishImages();
        }
    }
    
    /**
     * 批量上传鱼类图片
     */
    private void uploadFishImages() {
        try {
            // 鱼类名称列表（120种鱼类）
            List<String> fishNames = Arrays.asList(
                "鲤鱼", "鲫鱼", "草鱼", "青鱼", "鲢鱼", "鳙鱼", "鲶鱼", "黄颡鱼", "泥鳅", "鳝鱼",
                "黑鱼", "鲈鱼", "鳜鱼", "鲑鱼", "鲟鱼", "鲂鱼", "鲴鱼", "鲮鱼", "鲻鱼", "鲷鱼",
                "鲽鱼", "鲆鱼", "鲊鱼", "鲋鱼", "鲌鱼", "鲏鱼", "鲐鱼", "鲑鱼", "鲒鱼", "鲓鱼",
                "鲔鱼", "鲕鱼", "鲖鱼", "鲗鱼", "鲘鱼", "鲙鱼", "鲚鱼", "鲛鱼", "鲜鱼", "鲝鱼",
                "鲞鱼", "鲟鱼", "鲠鱼", "鲡鱼", "鲢鱼", "鲣鱼", "鲤鱼", "鲥鱼", "鲦鱼", "鲧鱼",
                "鲨鱼", "鲩鱼", "鲪鱼", "鲫鱼", "鲬鱼", "鲭鱼", "鲮鱼", "鲯鱼", "鲰鱼", "鲱鱼",
                "鲲鱼", "鲳鱼", "鲴鱼", "鲵鱼", "鲶鱼", "鲷鱼", "鲸鱼", "鲹鱼", "鲺鱼", "鲻鱼",
                "鲼鱼", "鲽鱼", "鲾鱼", "鲿鱼", "鳀鱼", "鳁鱼", "鳂鱼", "鳃鱼", "鳄鱼", "鳅鱼",
                "鳆鱼", "鳇鱼", "鳈鱼", "鳉鱼", "鳊鱼", "鳋鱼", "鳌鱼", "鳍鱼", "鳎鱼", "鳏鱼",
                "鳐鱼", "鳑鱼", "鳒鱼", "鳓鱼", "鳔鱼", "鳕鱼", "鳖鱼", "鳗鱼", "鳘鱼", "鳙鱼",
                "鳚鱼", "鳛鱼", "鳜鱼", "鳝鱼", "鳞鱼", "鳟鱼", "鳠鱼", "鳡鱼", "鳢鱼", "鳣鱼",
                "鳤鱼", "鳥鱼", "鳦鱼", "鳧鱼", "鳨鱼", "鳩鱼", "鳪鱼", "鳫鱼", "鳬鱼", "鳭鱼",
                "鳮鱼", "鳯鱼", "鳰鱼", "鳱鱼", "鳲鱼", "鳳鱼", "鳴鱼", "鳵鱼", "鳶鱼", "鳷鱼"
            );
            
            // 图片文件夹路径（请根据实际情况修改）
            String imageFolder = "D:/fish-images/"; // 修改为你的图片文件夹路径
            
            log.info("开始批量上传 {} 种鱼类图片...", fishNames.size());
            
            // 执行批量上传
            Map<String, Object> result = batchProcessor.batchUploadFishImages(imageFolder, fishNames);
            
            // 输出结果
            log.info("批量上传完成！");
            log.info("总数量: {}", result.get("totalCount"));
            log.info("成功数量: {}", result.get("successCount"));
            log.info("失败数量: {}", result.get("failedCount"));
            
            // 生成SQL更新语句
            @SuppressWarnings("unchecked")
            Map<String, String> successUploads = (Map<String, String>) result.get("successUploads");
            if (successUploads != null && !successUploads.isEmpty()) {
                List<String> sqlList = batchProcessor.generateUpdateSql(successUploads);
                
                log.info("生成的SQL更新语句:");
                for (String sql : sqlList) {
                    log.info(sql);
                }
                
                // 将SQL保存到文件
                saveSqlToFile(sqlList, "fish_image_update.sql");
            }
            
        } catch (Exception e) {
            log.error("鱼类图片批量上传失败", e);
        }
    }
    
    /**
     * 将SQL语句保存到文件
     */
    private void saveSqlToFile(List<String> sqlList, String fileName) {
        try {
            java.nio.file.Path filePath = java.nio.file.Paths.get(fileName);
            java.nio.file.Files.write(filePath, sqlList, java.nio.charset.StandardCharsets.UTF_8);
            log.info("SQL语句已保存到文件: {}", fileName);
        } catch (Exception e) {
            log.error("保存SQL文件失败", e);
        }
    }
}
